//
//  CZUserAccount.swift
//  WeiBo2
//
//  Created by Apple on 16/3/11.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

class CZUserAccount: NSObject, NSCoding {
    
    //  接口给的类型不对，应该是int类型
    //  用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    //  access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //  计算从获取到accesstoken那那一刻起，多少秒后过去，求出过去时间,在设置这个expires_in的时候计算出来过去日期
    var expiresDate: NSDate?
    
//    //  access_token的生命周期（该参数即将废弃，开发者请使用expires_in）。
//    var remind_in: Int = 0
    //  当前授权用户的UID。
    var uid: Int = 0
    //  头像
    var avatar_large: String?
    //  昵称
    var screen_name: String?
    
    init(dic: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dic)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String {
        let dic = ["access_token","expires_in","uid", "avatar_large", "screen_name","expiresDate"]
        //  通过keys获取对象的字典值
        return dictionaryWithValuesForKeys(dic).description
    }
    
    
    
    
    // MARK: -归档解档方法
    //  归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    //  解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        //  如果接档成功并且这个还是一个int类型赋值
        if let userId =  aDecoder.decodeObjectForKey("uid") as? Int {
            uid = userId
        }
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    
    
//    //  保存到沙盒目录里面
//    func saveUserInfo() {
//        
//        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
//        
//        NSKeyedArchiver.archiveRootObject(self, toFile: path)
//    
//    }
//    
//    //  读取沙盒目录返回实例
//    class func userAccount() -> CZUserAccount? {
//        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
//        return NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? CZUserAccount
//        
//    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
//    func encodeWithCoder(aCoder: NSCoder) {
//        aCoder.encodeObject(access_token, forKey: "access_token")
//        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
//        aCoder.encodeObject(uid, forKey: "uid")
//        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
//        aCoder.encodeObject(screen_name, forKey: "screen_name")
//    }
//    
//    required init?(coder aDecoder: NSCoder) {
//        access_token = aDecoder.decodeObjectForKey("access_token") as? String
//        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
//        //  如果转换成int成功，接档数据
//        if let userId = aDecoder.decodeObjectForKey("uid") as? Int {
//            uid = userId
//        }
//        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
//        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
//        
//    }
    
//    //  保存用户对象到沙盒目录里面
//    func saveUserInfo() {
//        
//        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userInfo.archive")
//        print(path)
//        //  执行归档
//        NSKeyedArchiver.archiveRootObject(self, toFile:path)
//        
//    }
//    
//    //  接档用户保存对象
//    class func getUserInfo() -> CZUserAccount? {
//        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userInfo.archive")
//        //  执行解档
//        return NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? CZUserAccount
//    }
    
    
}
